﻿using PersistentObject.Common.DataConvertors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace PersistentObject.Common.Schema
{
    public class Column
    {
        public Column()
        {
        }

        public Column(Table table, ColumnInfo info, int sequence)
        {
            Table = table;
            Property = info.Property;
            PropertyName = info.Property.Name;
            ColumnName = info.Name;
            Convertor = info.Convertor;
            Load = info.Load;
            Save = info.Save;
            IsKeyColumn = info.IsKeyColumn;
            IsAutoGenerated = info.IsAutoGenerated;
            Sequence = sequence;
            ForeignTableInfo = info.ForeignTableInfo;
            IsForeignKeyColumn = info.IsForeignKeyColumn;
        }

        /// <summary>
        /// Indicate the table info of this column info belonging to 
        /// </summary>
        public Table Table { get; internal set; }

        /// <summary>
        /// Indicate the property info of this column info belonging to
        /// </summary>
        public PropertyInfo Property { get; internal set; }

        /// <summary>
        /// Indicate the name of this column info
        /// </summary>
        public string ColumnName { get; internal set; }

        public string PropertyName { get; internal set; }

        public DataConvertor Convertor { get; internal set; }

        /// <summary>
        /// Indicate whether this column info can load data from database
        /// </summary>
        public bool Load { get; internal set; }

        /// <summary>
        /// Indicate whether this column info can save data into database
        /// </summary>
        public bool Save { get; internal set; }

        public bool IsKeyColumn { get; internal set; }

        public bool IsAutoGenerated { get; internal set; }

        public bool IsForeignKeyColumn { get; internal set; }

        public TableInfo ForeignTableInfo { get; internal set; }

        /// <summary>
        /// Indicate the table info of the foreign table
        /// </summary>
        public Table ForeignTable { get; internal set; }


        public int Sequence { get; internal set; }

        public override string ToString()
        {
            return string.Format("{0}.{1}, key:{2}, foreign:{3}, ", Table.TableName, ColumnName, IsKeyColumn,
                IsForeignKeyColumn ? ForeignTable.TableName : "");
        }
    }
}
